﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SystemControllers;
using eTMS.Controllers;
using eTMS.Commons;
using SystemControllers.UsersService;

namespace eTMS.Forms.Systems{
    public partial class FrmAddNewUsers: eTMS.Controls.BaseForm
    {
        public FrmAddNewUsers()
        {
            InitializeComponent();
        }
        #region Variables
        private UsersController controller=new UsersController();
        #endregion
        #region Screen Functions
        //Load All lable text
        private void LoadLanguage(){			
			this.Text=Constants.APP_ADDNEW+" "+UsersStringMapping.Users;
			this.labelFormTitle.Text=Constants.APP_ADDNEW+" "+UsersStringMapping.Users;
			labelUserName.Text=UsersStringMapping.UserName;			
			labelPassword.Text=UsersStringMapping.Password;			
			labelDisplayName.Text=UsersStringMapping.DisplayName;			
			labelUserType.Text=UsersStringMapping.UserType;			
			labelEmail.Text=UsersStringMapping.Email;			
			labelPhone.Text=UsersStringMapping.Phone;			
			labelEmployee_ID.Text=UsersStringMapping.Employee_ID;			
			labelIsOnline.Text=UsersStringMapping.IsOnline;			
			labelActived.Text=UsersStringMapping.Actived;			
        }
        //Copy data from control in screen to object
        private void CopyFromSreen(UsersEntity o){
			 o.UserName=Convert.ToString(txtUserName.Text);
			 o.Password=Convert.ToString(txtPassword.Text);
			 o.DisplayName=Convert.ToString(txtDisplayName.Text);
			 o.UserType=(byte)txtUserType.Value;
			 o.Email=Convert.ToString(txtEmail.Text);
			 o.Phone=Convert.ToString(txtPhone.Text);
			 o.Employee_ID=(int)txtEmployee_ID.Value;
			 o.IsOnline=txtIsOnline.Checked;
			 o.Actived=txtActived.Checked;
        }
        //Validate all data from control in screen before Save data
        protected bool ValidateScreen(){
		    bool result=true;
			if(txtUserName.Text.Trim()==""){
				errorProviderData.SetError (txtUserName, Constants.MSS_ERROR_TEXT);
				result=false;
			}			
			else{
				errorProviderData.SetError(txtUserName, "");
			}			
		    if(!result){
				ShowInfo(Constants.MSS_REQUIRE_FIELD);
		    }
		    return result;
        }
        #endregion
        #region Business
        //AddNew data to database
        private bool AddNewData()
        {
            controller.ResetObject();	
		    CopyFromSreen(controller.UsersObject);		    
            bool rval=controller.AddNew();
            return rval;
        }
        #endregion
        #region Windows Control EventHandlers
        private void FrmAddNewUsers_Load(object sender, EventArgs e)
        {
			LoadLanguage();
        }       
        private void ucCommand_OnOK(object sender, CancelEventArgs args)
        {
			if(!ValidateScreen()){
				args.Cancel = true;
				return;
			}            
            if(AddNewData()){
                ShowInfo(Constants.MSS_SAVE_SUCCESS);
            }
            else{
				args.Cancel = true;
                ShowWarning(Constants.MSS_SAVE_FAILE);
            }
        }
        private void ucCommand_OnCancel(object sender, CancelEventArgs args)
        {
        }
        #endregion
    }
}
